package fr.ivu;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class CoranRenamer {
	// @formatter:off
	static String NAMES = 
			"001 - el fatiha.mp3\r\n" + 
			"002 - el bakara.mp3\r\n" + 
			"003 - el omran.mp3\r\n" + 
			"004 - ennisah.mp3\r\n" + 
			"005 - al meida.mp3\r\n" + 
			"006 - Al-An'am.mp3\r\n" + 
			"007 - el aaraf.mp3\r\n" + 
			"008 - el anghal.mp3\r\n" + 
			"009 - ettoubah.mp3\r\n" + 
			"010 - younes.mp3\r\n" + 
			"011 - houd.mp3\r\n" + 
			"012 - yousef.mp3\r\n" + 
			"013 - erraed.mp3\r\n" + 
			"014 - ibrahim.mp3\r\n" + 
			"015 - el hajr.mp3\r\n" + 
			"016 - ennahl.mp3\r\n" + 
			"017 - el israe.mp3\r\n" + 
			"018 - el kahf.mp3\r\n" + 
			"019 - mariam.mp3\r\n" + 
			"020 - tah.mp3\r\n" + 
			"021 - el enbiyae.mp3\r\n" +  
			"022 - el hadj.mp3\r\n" + 
			"023 - el moeminoun.mp3\r\n" + 
			"024 - ennour.mp3\r\n" + 
			"025 - el forkhan.mp3\r\n" + 
			"026 - echchouaarae.mp3\r\n" + 
			"027 - ennaml.mp3\r\n" + 
			"028 - el kisas.mp3\r\n" + 
			"029 - el aankabout.mp3\r\n" + 
			"030 - erroum.mp3\r\n" + 
			"031 - lokmen.mp3\r\n" + 
			"032 - essajda.mp3\r\n" + 
			"033 - el ahzeeb.mp3\r\n" + 
			"034 - sebeh.mp3\r\n" + 
			"035 - faater.mp3\r\n" + 
			"036 - yees.mp3\r\n" + 
			"037 - essaafeet.mp3\r\n" + 
			"038 - saa.mp3\r\n" + 
			"039 - ezzamr.mp3\r\n" + 
			"040 - ghaafer.mp3\r\n" + 
			"041 - fassalet.mp3\r\n" + 
			"042 - echchoura.mp3\r\n" + 
			"043 - ezzokhrof.mp3\r\n" + 
			"044 - eddokhaan.mp3\r\n" + 
			"045 - el jaathia.mp3\r\n" + 
			"046 - el ahkaaf.mp3\r\n" + 
			"047 - mohammed.mp3\r\n" + 
			"048 - el fath.mp3\r\n" + 
			"049 - el hazaraat.mp3\r\n" + 
			"050 - kaa.mp3\r\n" + 
			"051 - eththeerieet.mp3\r\n" + 
			"052 - ettour.mp3\r\n" + 
			"053 - ennejm.mp3\r\n" + 
			"054 - el kamar.mp3\r\n" + 
			"055 - errahmeen.mp3\r\n" + 
			"056 - el oueekia.mp3\r\n" + 
			"057 - el hadiid.mp3\r\n" + 
			"058 - el moujeedileh.mp3\r\n" + 
			"059 - el hachar.mp3\r\n" + 
			"060 - el momtehineh.mp3\r\n" + 
			"061 - essaf.mp3\r\n" + 
			"062 - el joumouaa.mp3\r\n" + 
			"063 - el mouneefikoun.mp3\r\n" + 
			"064 - ettaghabon.mp3\r\n" + 
			"065 - ettaleek.mp3\r\n" + 
			"066 - ettahriim.mp3\r\n" + 
			"067 - el malak.mp3\r\n" + 
			"068 - el kalem.mp3\r\n" + 
			"069 - el haakkah.mp3\r\n" + 
			"070 - el maaaredj.mp3\r\n" + 
			"071 - nououh.mp3\r\n" + 
			"072 - el jinn.mp3\r\n" + 
			"073 - el mezmel.mp3\r\n" + 
			"074 - el medthir.mp3\r\n" + 
			"075 - el kieemah.mp3\r\n" + 
			"076 - el inseen.mp3\r\n" + 
			"077 - el morsaleet.mp3\r\n" + 
			"078 - ennabeh.mp3\r\n" + 
			"079 - enneeziaat.mp3\r\n" + 
			"080 - abes.mp3\r\n" + 
			"081 - ettekouiir.mp3\r\n" + 
			"082 - el infitaar.mp3\r\n" + 
			"083 - el moutaffifiin.mp3\r\n" + 
			"084 - el inchikaak.mp3\r\n" + 
			"085 - el bourououj.mp3\r\n" + 
			"086 - ettaarik.mp3\r\n" + 
			"087 - el aaalee.mp3\r\n" + 
			"088 - el ghaachiah.mp3\r\n" + 
			"089 - el fadjer.mp3\r\n" + 
			"090 - el balad.mp3\r\n" + 
			"091 - echchems.mp3\r\n" + 
			"092 - elleil.mp3\r\n" + 
			"093 - edhdhouhaa.mp3\r\n" + 
			"094 - echchareh.mp3\r\n" + 
			"095 - ettiin.mp3\r\n" + 
			"096 - el alek.mp3\r\n" + 
			"097 - el kadar.mp3\r\n" + 
			"098 - el baiiinah.mp3\r\n" + 
			"099 - ezzelzeleh.mp3\r\n" + 
			"100 - el aadieet.mp3\r\n" + 
			"101 - el kaariah.mp3\r\n" + 
			"102 - ettekeethour.mp3\r\n" + 
			"103 - el aasr.mp3\r\n" + 
			"104 - el hemzah.mp3\r\n" + 
			"105 - el fiil.mp3\r\n" + 
			"106 - kouraiich.mp3\r\n" + 
			"107 - el maaoun.mp3\r\n" + 
			"108 - el kaouthar.mp3\r\n" + 
			"109 - el keefirououn.mp3\r\n" + 
			"110 - ennasr.mp3\r\n" + 
			"111 - el masad.mp3\r\n" + 
			"112 - el ikhlaas.mp3\r\n" + 
			"113 - el falak.mp3\r\n" + 
			"114 - ennes.mp3";
	// @formatter:on

	public static void main(String[] args) {
		Map<Integer, String> nameIndexMap = constructNameIndex();
		if (args.length != 2) {
			System.err.println("Usage:\n\t java " + CoranRenamer.class
					+ " [directory of coran files] [true (to execute renaming) | 'any chars' (only preview results)]");
		}

		String filesDir = args[0];
		boolean execute = args[1].equals("true");
		System.out.println("----------------- Starting the processing of " + filesDir);
		File file = new File(filesDir);
		String rootAbsolutePath = file.getAbsolutePath();
		String[] fileList = file.list();
		for (int i = 0; i < fileList.length; i++) {
			String currentFileName = fileList[i];
			System.out.println(currentFileName);
			int index = getIndex(currentFileName);
			if (index == -1) {
				System.err.println("Skipping file " + currentFileName + ". No index found in name");

				continue;
			}

			String souratName = nameIndexMap.get(index);
			if (souratName == null) {
				System.err.println("No sourate name found for index " + index + " (file: '" + currentFileName + "')");
				continue;
			}

			String newName = String.format("%03d", index) + " - " + souratName + ".mp3";

			File currentFile = new File(rootAbsolutePath + System.getProperty("file.separator") + currentFileName);
			File currentFileNewName = new File(rootAbsolutePath + System.getProperty("file.separator") + newName);
			System.out.println((execute ? "Renaming" : "Simulating") + " '" + currentFile + "' to '" + currentFileNewName + "'");

			if (execute)
				currentFile.renameTo(currentFileNewName);
		}
	}

	protected static int getIndex(String currentFileName) {
		String substring = currentFileName.substring(0, 3);
		int index;
		try {
			index = Integer.parseInt(substring);
		} catch (NumberFormatException e) {
			try {
				index = Integer.parseInt(substring.substring(0, 2));
			} catch (NumberFormatException ex) {
				try {
					index = Integer.parseInt(substring.substring(0, 1));
				} catch (NumberFormatException ex1) {
					return -1;
				}
			}
		}
		return index;
	}

	protected static Map<Integer, String> constructNameIndex() {
		String[] names = NAMES.split("\r\n");
		Map<Integer, String> nameIndexMap = new HashMap<Integer, String>();
		for (int i = 0; i < names.length; i++) {
			String name = names[i];
			int index = Integer.parseInt(name.substring(0, 3));
			if (index != i + 1)
				throw new IllegalStateException("missing sourate " + index);
			int end = name.toLowerCase().indexOf(".mp3");
			int start = "xxx - ".length();
			name = name.substring(start, end);
			System.out.println("name " + (i + 1) + ":\t " + name);
			nameIndexMap.put(index, name);
		}
		return nameIndexMap;
	}
}
